package com.jorado.etl.util;

import com.jorado.event.EventClient;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.DFSInputStream;
import org.apache.hadoop.hdfs.server.namenode.NameNode;
import org.apache.hadoop.io.IOUtils;

import java.io.*;
import java.util.Map;

/**
 * @author len.zhang
 */
public class HDFSClient implements AutoCloseable {

    private static final int BUFFER_SIZE = 4096;
    private DFSClient dfsClient;
    private Configuration dfsConfig;

    public HDFSClient(String dfsPath) throws IOException {
        System.setProperty("HADOOP_USER_NAME", "hdfs");
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", dfsPath);
        this.dfsConfig = conf;
        this.dfsClient = new DFSClient(NameNode.getAddress(dfsConfig), dfsConfig);
    }

    public HDFSClient(Configuration dfsConfig) {
        this.dfsConfig = dfsConfig;
    }

    /**
     * 提交文件
     *
     * @param local
     * @param remote
     * @return
     * @throws IOException
     */
    public HDFSClient upload(String local, String remote) throws IOException {

        InputStream in = new BufferedInputStream(new FileInputStream(local));

        OutputStream out = dfsClient.create(remote, true);

        IOUtils.copyBytes(in, out, BUFFER_SIZE, true);

        return this;
    }

    /**
     * 批量提交文件
     *
     * @param fileMap
     * @return
     * @throws IOException
     */
    public HDFSClient upload(Map<String, String> fileMap) throws IOException {

        for (Map.Entry<String, String> entry : fileMap.entrySet()) {
            this.upload(entry.getKey(), entry.getValue());
        }

        return this;
    }

    /**
     * 下载文件到本地
     *
     * @param remote
     * @param local
     * @return
     * @throws IOException
     */
    public HDFSClient download(String remote, String local) throws IOException {

        DFSInputStream in = this.dfsClient.open(remote);

        OutputStream out = new FileOutputStream(local);

        IOUtils.copyBytes(in, out, BUFFER_SIZE, true);

        return this;
    }

    /**
     * 批量下载文件
     *
     * @param fileMap
     * @return
     * @throws IOException
     */
    public HDFSClient download(Map<String, String> fileMap) throws IOException {

        for (Map.Entry<String, String> entry : fileMap.entrySet()) {
            this.download(entry.getKey(), entry.getValue());
        }
        return this;
    }

    /**
     * 批量删除文件
     *
     * @param paths
     * @return
     * @throws IOException
     */
    public HDFSClient delete(String... paths) throws IOException {

        for (String path : paths) {
            if (dfsClient.exists(path)) {
                dfsClient.delete(path, true);
            }
        }
        return this;
    }

    /**
     * 是否存在文件
     *
     * @param path
     * @return
     * @throws IOException
     */
    public boolean exists(String path) throws IOException {

        return dfsClient.exists(path);
    }

    /**
     * 关闭链接
     *
     * @throws IOException
     */
    @Override
    public void close() {

        if (null == this.dfsClient) {
            return;
        }

        try {
            this.dfsClient.close();
        } catch (Exception ex) {
            EventClient.getDefault().asyncSubmitException("Close dfs client error", ex);
        }

    }

    public DFSClient getClient() {
        return dfsClient;
    }

    public Configuration getConfig() {
        return dfsConfig;
    }
}
